package com.gp.controller.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gp.common.result.JsonResult;
import com.gp.common.utils.CookieUtils;
import com.gp.controller.BaseController;
import com.gp.model.entity.Areas;
import com.gp.model.entity.BaseAddress;
import com.gp.model.entity.BaseUser;
import com.gp.service.IAreasService;
import com.gp.service.IBaseAddressService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * @author: 弓成龙
 * @date : 2017年12月19日
 * @email : gcx909109@sina.cn
 * @description:
 */

@RestController
@Api(description = "收货地址", value = "收货地址")
@RequestMapping("/address")
public class AddressController extends BaseController {
	@Autowired
	private IBaseAddressService addressService;
	@Autowired
	private IAreasService areaService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ApiOperation(value = "获得当前用户的所有收货地址", httpMethod = "GET")
	public JsonResult addressList() {
		List<BaseAddress> list = new ArrayList<BaseAddress>();
		Map<String, Object> columnMap = new HashMap<String, Object>();
		if (CookieUtils.isExistsCurrentUser(CookieUtils.getCurrentRequest())) {
			columnMap.put("user_id",
					((BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest())).getId());
			list = addressService.selectByMap(columnMap);
			return new JsonResult("执行成功", list);
		} else {
			return new JsonResult("500", "当前用户不存在");
		}
	}

	@RequestMapping(value = "/detail/{addressId}", method = RequestMethod.GET)
	@ApiOperation(value = "通过id获得收货地址", httpMethod = "GET")
	public JsonResult address(@PathVariable Long addressId) {
		BaseAddress address1 = addressService.selectById(addressId);
		return new JsonResult("执行成功", address1);
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "添加收货地址", httpMethod = "POST")
	public JsonResult addAddress(BaseAddress address) {
		address.setUserId(((BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest())).getId());
		// 如果用户增加的地址为默认地址，重置该用户所有的默认地址
		if (address.getIsDefault() == 1) {
			addressService.resetAddress(((BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest())));
		}
		int result = addressService.insertAddress(address);
		return result == 1 ? new JsonResult("执行成功", result) : new JsonResult("添加失败", result);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ApiOperation(value = "编辑收货地址", httpMethod = "POST")
	public JsonResult updateAddress(BaseAddress address) {
	    if(address.getIsDefault()==1) {
	   // 设置该用户所有的地址默认为0
	        addressService.resetAddress(((BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest())));
	    }
		boolean result = addressService.updateById(address);
		return result ? new JsonResult("修改成功", result) : new JsonResult("修改失败", result);
	}

	@RequestMapping(value = "/del/{addressId}", method = RequestMethod.POST)
	@ApiOperation(value = "删除收货地址", httpMethod = "POST")
	public JsonResult delAddress(@PathVariable Long addressId) {
		boolean result = addressService.deleteById(addressId);
		return result ? new JsonResult("删除成功", result) : new JsonResult("删除失败", result);
	}

	@RequestMapping(value = "/isDefault/{addressId}", method = RequestMethod.POST)
	@ApiOperation(value = "设置默认收货地址", httpMethod = "POST")
	public JsonResult updateDefaultAddress(@PathVariable Long addressId) {
		boolean result = addressService.deleteById(addressId);
		// 设置该用户所有的地址默认为0
		addressService.resetAddress(((BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest())));
		// 设置当前的地址为默认
		BaseAddress entity = new BaseAddress();
		entity.setAddressId(addressId);
		entity.setIsDefault(1);
		addressService.updateById(entity);
		return result ? new JsonResult("删除成功", result) : new JsonResult("删除失败", result);
	}

	@RequestMapping(value = "/area/{level}", method = RequestMethod.GET)
	@ApiOperation(value = "收货地址", httpMethod = "GET")
	public JsonResult listArea(@NotNull @PathVariable Integer level) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (level <= 3) {
			map.put("LevelType", level);
			List<Areas> selectByMap = areaService.selectByMap(map);
			if (selectByMap == null || selectByMap.isEmpty()) {
				return renderError("数据不存在");
			} else {
				return renderSuccess("查询成功", selectByMap);
			}
		} else {
			map.put("parentId", level);
			List<Areas> selectByMap = areaService.selectByMap(map);
			if (selectByMap == null || selectByMap.isEmpty()) {
				return renderError("数据不存在");
			} else {
				return renderSuccess("查询成功", selectByMap);
			}
		}
	}

}
